package com.tianzeculture.stagebao.net.base;


import com.google.gson.Gson;
import com.tianzeculture.stagebao.application.StageBaoApplication;
import com.tianzeculture.stagebao.network.net.httpclient.BaseRestUsage;
import com.tianzeculture.stagebao.network.net.httpclient.NewCustomResponseHandler;
import com.tianzeculture.stagebao.network.net.httpclient.httputil.HttpInterceptor;
import com.tianzeculture.stagebao.network.net.httpclient.httputil.SimpleHttpInterceptor;
import com.tianzeculture.stagebao.util.FileUtil;
import com.tianzeculture.stagebao.util.VKConstants;

import java.util.HashMap;
import java.util.Map;

/**
 * 网络请求
 */
public class AppBaseRestUsageV2 extends BaseRestUsage {

//    private static String BASE_URL = "http://192.168.0.103:8080";//测试环境
//    public static String PICTURE_URL = "http://192.168.0.103:8080";//测试图片环境
//    public static String BASE_URL = "http://47.106.116.243:8080";//线上环境
//    public static String PICTURE_URL = "http://47.106.116.243:8081";//线上图片环境
    public static String BASE_URL = "http://www.wtb.wutaibao.com";//线上域名环境
    public static String PICTURE_URL = "http://www.pri.wutaibao.com";//线上域名图片环境

    /**
     *
     */
    protected Gson gson = new Gson();

    @Override
    protected HttpInterceptor getHttpInterceptor() {

        return new SimpleHttpInterceptor() {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> headerMap = new HashMap<>();
                headerMap.put("Content-Type", "application/json");
                headerMap.put("token", "");//用户登录标记
                headerMap.put("mode", "Android");//设备类型（安卓，iOS）
//                headerMap.put("device", DEVICE_ID);//设备号
                headerMap.put("device", "1");//设备号
                headerMap.put("version", "1");//版本信息
                headerMap.put("Uid", "");//用户id
//                Log.e("======请求头信息======", headerMap.size() + "=====" + headerMap.toString());
                return headerMap;
            }

            @Override
            public String getCacheDir() {
                return FileUtil.getDirectory(VKConstants.CACHE_DATA).getAbsolutePath();
            }

            @Override
            public String getBaseUrl() {
                return BASE_URL;
            }
        };

    }

    /**
     * post请求
     *
     * @param relativeUrl
     * @param object
     * @param responseHandler
     */
    private void requestPost(String relativeUrl, Object object, boolean isCache, NewCustomResponseHandler responseHandler) {
        if (responseHandler != null) {
            responseHandler.setIdentification(getIdentification());
        }
        super.post(StageBaoApplication.getInstance(), relativeUrl, object, isCache, responseHandler);
    }

    /**
     * post请求
     *
     * @param relativeUrl
     * @param responseHandler
     */
    private void requestGet(String relativeUrl, Map<String, String> paramMap, boolean isCache, NewCustomResponseHandler responseHandler) {
        if (responseHandler != null) {
            responseHandler.setIdentification(getIdentification());
        }

        super.get(relativeUrl, paramMap, isCache, responseHandler);
    }

    /**
     * post请求 没缓存
     *
     * @param relativeUrl
     * @param object
     * @param responseHandler
     */
    public void post(String relativeUrl, Object object, NewCustomResponseHandler responseHandler) {

        requestPost(relativeUrl, object, false, responseHandler);
    }

    /**
     * post请求 有缓存
     *
     * @param relativeUrl
     * @param object
     * @param responseHandler
     */
    public void postCache(String relativeUrl, Object object, NewCustomResponseHandler responseHandler) {
        requestPost(relativeUrl, object, true, responseHandler);
    }

    /**
     * get请求 没缓存
     *
     * @param relativeUrl
     * @param responseHandler
     */
    public void get(String relativeUrl, Map<String, String> paramMap, NewCustomResponseHandler responseHandler) {
        requestGet(relativeUrl, paramMap, false, responseHandler);
    }

    /**
     * get请求 有缓存
     *
     * @param relativeUrl
     * @param responseHandler
     */
    public void getCache(String relativeUrl, Map<String, String> paramMap, NewCustomResponseHandler responseHandler) {
        requestGet(relativeUrl, paramMap, true, responseHandler);
    }

}
